package org.csu.myPetStore.web.servlet.Login;

import org.csu.myPetStore.domain.Account;
import org.csu.myPetStore.domain.Cart;
import org.csu.myPetStore.service.AccountService;
import org.csu.myPetStore.service.CartService;
import org.csu.myPetStore.service.LogcatService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;

public class LoginAccountServlet extends HttpServlet {

    private static final String LOGIN = "/WEB-INF/jsp/account/SignonForm.jsp";
    static final String MAIN = "/WEB-INF/jsp/catalog/Main.jsp";


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");

        LogcatService logcatService = new LogcatService();

        AccountService accountService = new AccountService();
        Account account = accountService.getAccount(username,password);

        CartService service = new CartService();
        HttpSession session = req.getSession();

        String code1 = (String) session.getAttribute("code");

        if(account == null){
            session.setAttribute("logMsg","用户不存在或密码错误");
            int num = (int) session.getAttribute("loginTime") + 1;
            session.setAttribute("loginTime",num);
            req.getRequestDispatcher(LOGIN).forward(req,resp);
        }else{
            if(code1!=null && code!=null){
                if(!code.equals(code1) && !code.equals("")) {
                    session.setAttribute("logMsg", "验证码错误");
                    req.getRequestDispatcher(LOGIN).forward(req, resp);
                }else{
                    //放一个account便于追踪
                    session.setAttribute("loginTime",0);
                    session.setAttribute("account",account);
                    session.setAttribute("accountMsg","当前用户"+account.getUsername());
                    Cart cart = service.getCartByUsername(account.getUsername());
                    session.setAttribute("cart",cart);

                    //添加日志信息
                    java.util.Date date = new java.util.Date();
                    Date datesql = new Date(date.getTime());
                    logcatService.InsertLogCat(account.getUsername(),account.getUsername() + " Login " + " Successfully at " + datesql.toString());

                    session.setAttribute("logMsg",null);

                    req.getRequestDispatcher(MAIN).forward(req,resp);
                }
            }else{
                //放一个account便于追踪
                session.setAttribute("loginTime",0);
                session.setAttribute("account",account);
                session.setAttribute("accountMsg","当前用户"+account.getUsername());
                Cart cart = service.getCartByUsername(account.getUsername());
                session.setAttribute("cart",cart);

                //添加日志信息
                java.util.Date date = new java.util.Date();
                Date datesql = new Date(date.getTime());
                logcatService.InsertLogCat(account.getUsername(),account.getUsername() + " Login " + " Successfully at " + datesql.toString());

                session.setAttribute("logMsg",null);
                req.getRequestDispatcher(MAIN).forward(req,resp);
            }
        }

    }
}
